package com.hanhai.material.device.controller;

import com.alibaba.fastjson.JSONObject;
import com.hanhai.material.common.result.Result;
import com.hanhai.material.common.result.ResultTool;
import com.hanhai.material.device.entity.SysBadRecord;
import com.hanhai.material.device.entity.SysReason;
import com.hanhai.material.device.service.SysBadRecordService;
import com.hanhai.material.device.service.SysReasonService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.hanhai.material.device.entity.table.SysBadRecordTableDef.SYS_BAD_RECORD;
import static com.hanhai.material.device.entity.table.SysReasonTableDef.SYS_REASON;
import static com.hanhai.material.utils.TimeUtils.fromDateToString;
import static com.mybatisflex.core.query.QueryMethods.distinct;
import static com.mybatisflex.core.query.QueryMethods.sum;

/**
 *  控制层。
 *
 * @author youchuang
 * @since 2023/10/18 11:26:03
 */
@RestController
@RequestMapping("/reason")
@Log4j2
public class SysReasonController {

    @Autowired
    private SysReasonService sysReasonService;

    @Autowired
    private SysBadRecordService sysBadRecordService;

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    public Page<SysReason> page(Page<SysReason> page) {
        return sysReasonService.page(page);
    }

    @PostMapping("/list/{workShop}")
    public Result<?> getList(@PathVariable String workShop,@RequestBody SysBadRecord sysBadRecord){
        log.error("SysBadRecord:{}", JSONObject.toJSONString(sysBadRecord));
        QueryWrapper queryWrapper=QueryWrapper.create();

        queryWrapper.select(distinct(SYS_REASON.REASON_TYPE));
        List<SysReason> sysReasonList=this.sysReasonService.list(queryWrapper);
        for(SysReason sysReason:sysReasonList){
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(SYS_BAD_RECORD.MACHINE_NAME.eq(sysBadRecord.getMachineName()));
            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysBadRecord.getShift()));
            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.eq(sysReason.getReasonType()));
            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(sysBadRecord.getWorkPlanId()));
            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysBadRecord.getProductDate())));
            queryWrapper.select(sum(SYS_BAD_RECORD.NUM).as("num"));
            SysBadRecord badRecord=this.sysBadRecordService.getOne(queryWrapper);
            log.error("SQL:{}",queryWrapper.toSQL());
            if(badRecord!=null){
                log.error("有不良");
                sysReason.setTotalNum(badRecord.getNum());
            }else{
                sysReason.setTotalNum(0);
            }
        }
        return sysReasonList.isEmpty()?ResultTool.fail():ResultTool.success(sysReasonList);
    }

    @PostMapping("/list/{workShop}/{reasonType}")
    public Result<?> getReasonList(@PathVariable String workShop, @PathVariable String reasonType, @RequestBody SysBadRecord sysBadRecord){
        log.error("");
        QueryWrapper queryWrapper=QueryWrapper.create();
//        queryWrapper.where(SYS_REASON.REASON_W)
        queryWrapper.where(SYS_REASON.REASON_WORK_SHOP.eq(workShop)).and(SYS_REASON.REASON_TYPE.eq(reasonType));
        List<SysReason> sysReasonList=this.sysReasonService.list(queryWrapper);
        for(SysReason sysReason:sysReasonList){
            queryWrapper=QueryWrapper.create();
            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysBadRecord.getProductDate())));
            queryWrapper.where(SYS_BAD_RECORD.MACHINE_NAME.eq(sysBadRecord.getMachineName()));
            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysBadRecord.getShift()));
            queryWrapper.where(SYS_BAD_RECORD.WORK_SHOP_NAME.eq(sysBadRecord.getWorkShopName()));
//            queryWrapper.where(SYS_BAD_RECORD.WORK_SHOP.eq(sysBadRecord.getWorkShop()));
            queryWrapper.where(SYS_BAD_RECORD.REASON_TYPE.eq(sysReason.getReasonType()));
            queryWrapper.where(SYS_BAD_RECORD.REASON_NAME.eq(sysReason.getReasonName()));
            queryWrapper.where(SYS_BAD_RECORD.WORK_PLAN_ID.eq(sysBadRecord.getWorkPlanId()));
            queryWrapper.where(SYS_BAD_RECORD.PRODUCT_DATE.eq(fromDateToString("yyyy-MM-dd",sysBadRecord.getProductDate())));
            queryWrapper.where(SYS_BAD_RECORD.SHIFT.eq(sysBadRecord.getShift()));
            queryWrapper.select(sum(SYS_BAD_RECORD.NUM).as("num"));
            SysBadRecord badRecord=this.sysBadRecordService.getOne(queryWrapper);
            log.error("SQL:{}",queryWrapper.toSQL());
            if(badRecord!=null){
                log.error("有不良");
                sysReason.setTotalNum(badRecord.getNum());
            }else{
                sysReason.setTotalNum(0);
            }
        }
        return sysReasonList.isEmpty()?ResultTool.fail():ResultTool.success(sysReasonList);
    }

}
